package com.shreecon.lms.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.shreecon.lms.interfaces.IMessageConstant;
import com.shreecon.lms.interfaces.LMSException;

/**
 * The Class DateUtil.
 * 
 * @author Santhosh. Modified on Aug 13, 2011
 */
public class DateUtil {

	/** The date format. */
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
	
	/**
     * Format.
     * 
     * @param date
     *            the date
     * @return the string
     */
	public static String format(Date date){
		return dateFormat.format(date);
	}
	
	/**
     * Parse_ d d_ m m_ yyyy.
     * 
     * @param dateInString
     *            the date in string
     * @return the date
     * @throws LMSException
     *             the lMS exception
     */
	public static Date parse_DD_MM_YYYY(String dateInString) throws LMSException {
	    if( dateInString == null ) {
		throw new LMSException(IMessageConstant.INVALID_DATE);
	    }
	    try {
		Date date = dateFormat.parse(dateInString);
		return date;
	    } catch (ParseException e) {
		throw new LMSException(IMessageConstant.INVALID_DATE);
	    } 
	}
	
	public static void validateDate (String dateInString,String field) throws LMSException {
	    try {
		parse_DD_MM_YYYY(dateInString);
	    } catch (LMSException e) {
		throw new LMSException(IMessageConstant.INVALID_DATE_FOR.concat(field));
	    }
	}
}
